﻿<UserControl
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
	xmlns:vm="clr-namespace:SqlExport.ViewModel"
	xmlns:view="clr-namespace:SqlExport.View"
	xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
    xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
	xmlns:Converters="clr-namespace:SqlExport.View.Converters" mc:Ignorable="d"
	x:Class="SqlExport.View.MessageList"
	x:Name="UserControl"
	d:DesignWidth="640" d:DesignHeight="480">
	<UserControl.Resources>
		<ResourceDictionary>
			<ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <ResourceDictionary.MergedDictionaries>
                        <ResourceDictionary Source="../Themes/Generic.xaml"/>
                    </ResourceDictionary.MergedDictionaries>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
			
			<view:BooleanToVisibilityConverter x:Key="ShowForSeparator"/>
            <view:BooleanToVisibilityConverter x:Key="HideForSeparator" Not="True"/>
            <Converters:ImageConverter x:Key="ImageConverter"/>
			<vm:MessageListViewModel x:Key="ViewModel" />
			<DataTemplate x:Key="MessageItemTemplate">
				<Grid Margin="0" VerticalAlignment="Top">
					<Grid.RowDefinitions>
						<RowDefinition/>
						<RowDefinition/>
					</Grid.RowDefinitions>
					<Grid.ColumnDefinitions>
						<ColumnDefinition Width="16"/>
						<ColumnDefinition/>
					</Grid.ColumnDefinitions>
					<Image Grid.ColumnSpan="1" Source="{Binding Image, Converter={StaticResource ImageConverter}, Mode=OneWay}" Visibility="{Binding IsSeparator, Converter={StaticResource HideForSeparator}, Mode=OneWay}" Stretch="None" VerticalAlignment="Top"/>
					<TextBlock Margin="4,0,0,0" TextWrapping="Wrap" d:LayoutOverrides="Width, Height" Grid.Column="1" Text="{Binding DisplayText}" Visibility="{Binding IsSeparator, Converter={StaticResource HideForSeparator}, Mode=OneWay}"/>
					<Rectangle Fill="#FFF4F4F5" Height="Auto" Stroke="Black" VerticalAlignment="Bottom" Grid.Row="1" Grid.ColumnSpan="2" Visibility="{Binding IsSeparator, Converter={StaticResource ShowForSeparator}, Mode=OneWay}" Margin="0,2,0,3"/>
				</Grid>
			</DataTemplate>
		</ResourceDictionary>
	</UserControl.Resources>
	<UserControl.DataContext>
        <Binding Path="MessageListViewModel" Source="{StaticResource Locator}"/>
	</UserControl.DataContext>

	<DockPanel x:Name="LayoutRoot">

		<Button x:Name="btnClear" Content="Clear" VerticalAlignment="Top" Width="75" DockPanel.Dock="Bottom" HorizontalAlignment="Right" Margin="5,5,0,0" Command="{Binding ClearCommand, Mode=OneWay}"/>

		<Grid>
			<Grid.ColumnDefinitions>
				<ColumnDefinition Width="0.5*"/>
				<ColumnDefinition Width="0.5*"/>
			</Grid.ColumnDefinitions>
			<GridSplitter Width="5"/>
			<ListView x:Name="lstErrors" Margin="0,0,5,0" 
                      ItemsSource="{Binding Messages}"
                      SelectedItem="{Binding SelectedItem}"
                      SelectionMode="Extended"
                      IsSynchronizedWithCurrentItem="True"
                      view:ListExtenders.AutoScrollToCurrentItem="True"
                      ItemTemplate="{DynamicResource MessageItemTemplate}"
                      HorizontalContentAlignment="Stretch">
                <ListView.ItemContainerStyle>
                    <Style TargetType="{x:Type ListViewItem}">
                        <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
                    </Style>
                </ListView.ItemContainerStyle>
                <i:Interaction.Triggers>
					<i:EventTrigger EventName="SelectionChanged">
						<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding ItemSelectionChangedCommand}"/>
					</i:EventTrigger>
					<i:EventTrigger EventName="MouseDoubleClick">
						<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding ItemExecuteCommand}" />
					</i:EventTrigger>
				</i:Interaction.Triggers>
			</ListView>
			<TextBox Grid.Column="1" TextWrapping="Wrap" d:LayoutOverrides="Width" Text="{Binding DetailsText}"/>
		</Grid>
	</DockPanel>
</UserControl>